package com.cy.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.cy.utils.PropertyUtil;
import com.jfinal.plugin.druid.DruidPlugin;
import jodd.util.StringUtil;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-8-26
 * Time: 下午4:03
 * To change this template use File | Settings | File Templates.
 */
public class DruidPluginConfig {


    public static DruidPlugin getDruidPlugin() {

        DruidPlugin druidPlugin = new DruidPlugin(getDbConfig("url"), getDbConfig("username"), getDbConfig("password"), getDbConfig("driverClass"));

        if(StringUtil.isNotBlank(getDbConfig("MaxActive"))) {
            druidPlugin = druidPlugin.setMaxActive(Integer.parseInt(getDbConfig("MaxActive")));
        }
        if(StringUtil.isNotBlank(getDbConfig("MinIdle"))) {
            druidPlugin = druidPlugin.setMinIdle(Integer.parseInt(getDbConfig("MinIdle")));
        }
        if(StringUtil.isNotBlank(getDbConfig("MaxActive"))) {
            druidPlugin = druidPlugin.setInitialSize(Integer.parseInt(getDbConfig("InitialSize")));
        }
        if(StringUtil.isNotBlank(getDbConfig("MinEvictableIdleTimeMillis"))) {
            druidPlugin = druidPlugin.setMinEvictableIdleTimeMillis(Integer.parseInt(getDbConfig("MinEvictableIdleTimeMillis")));
        }
        if(StringUtil.isNotBlank(getDbConfig("MaxPoolPreparedStatementPerConnectionSize"))) {
            druidPlugin = druidPlugin.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(getDbConfig("MaxPoolPreparedStatementPerConnectionSize")));
        }
        if(StringUtil.isNotBlank(getDbConfig("timeBetweenEvictionRunsMillis"))) {
            druidPlugin = druidPlugin.setTimeBetweenEvictionRunsMillis(Integer.parseInt(getDbConfig("timeBetweenEvictionRunsMillis")));
        }
        if(StringUtil.isNotBlank(getDbConfig("minEvictableIdleTimeMillis"))) {
            druidPlugin = druidPlugin.setMinEvictableIdleTimeMillis(Integer.parseInt(getDbConfig("minEvictableIdleTimeMillis")));
        }
        return druidPlugin;
    }

    private static String getDbConfig(String key) {
        return PropertyUtil.getGrosseValue(PropertyUtil.DB, key);
    }
}
